package Character;

import java.util.StringTokenizer;

import org.json.JSONObject;

import GameTools.ydMath;
import GameTools.ydPoint;
import android.graphics.Canvas;

import com.ydDev.charateranimation.R;

public class Torso {

	BodyPart torso;

	public Torso(ydPoint position) {
		torso = new BodyPart(position, R.drawable.gc1_torso);

		setJoints();

	}
	
	public void setJoints(){
		torso.setFirstJoint(new ydPoint(torso.limb.p1.x+0.028f*torso.skin.getHeight(), torso.limb.p1.y + 0.206f*torso.skin.getWidth()));
		torso.setSecondJoint(new ydPoint(torso.limb.p1.x+0.028f*torso.skin.getHeight(), torso.limb.p1.y + 0.933f*torso.skin.getWidth()));
	}

	public void draw(Canvas canvas) {
		torso.draw(canvas);
	}

	public void rotate(double rads, int mode) {
		if (mode == 0) {
			torso.rotate(rads);
		}
	}

	public void pointTo(ydPoint clickedAt, int jointSelected) {
		if(jointSelected==0){
			ydPoint currentSlope=torso.limb.slope;
			ydPoint newSlope=ydMath.slopeOf(torso.j1, clickedAt);
			
			double deltaRads=ydMath.vectorToRads(newSlope)-ydMath.vectorToRads(currentSlope);
			torso.rotate(deltaRads,0);

		}
		else if(jointSelected==1){
			ydPoint currentSlope=torso.limb.slope;
			ydPoint newSlope=ydMath.slopeOf(clickedAt, torso.j2);
			
			double deltaRads=ydMath.vectorToRads(newSlope)-ydMath.vectorToRads(currentSlope);
			torso.rotate(deltaRads,1);
		}
		
	}

	public void loadPostion(JSONObject json) {
		
/*		torso.limb.setSlope(ydPoint.stringPairToydPoint(json.optString("slope")));
		torso.limb.moveTo(ydPoint.stringPairToydPoint(json.optString("p1")));*/
		torso.limb.setPosition(ydPoint.stringPairToydPoint(json.optString("p1")), ydPoint.stringPairToydPoint(json.optString("p2")));
		torso.j1.setCoordinates(ydPoint.stringPairToydPoint(json.optString("j1")));
		torso.j2.setCoordinates(ydPoint.stringPairToydPoint(json.optString("j2")));
		
		//torso.limb.recalculate();
		
		return;
		
	}

	public void rotate(int direction) {
				if(direction==0){
					torso.rotate(torso.angularVelocity);
				}
				else if(direction==1){
					torso.rotate(-torso.angularVelocity);
				}
		
	}

	public void deltaMove(ydPoint shift) {
		torso.deltaMove(shift);
		
	}

	public void setScale(float s) {
		torso.setScale(s);
		setJoints();
	}

}
